﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Runtime.InteropServices;
namespace Adc_Display
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);//系统dll导入ini写函数
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);//系统dll导入ini读函数
        string FileName = System.AppDomain.CurrentDomain.BaseDirectory + "data.ini";//ini文件名
        StringBuilder temp = new StringBuilder(255);//存储读出ini内容变量
        string CurrentPortName;

        public Form1()
        {
            InitializeComponent();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(PortDataReceivedEvent); //串口数据接收事件 
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; //
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosing);
        }

        private void Form1_FormClosing(object sender,EventArgs e)
        {
            WritePrivateProfileString("PortData", "PortName", CurrentPortName, FileName);//窗口关闭，保存
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new Size(620, 107);//缩小
            GetPrivateProfileString("PortData", "PortName", "COM1", temp, 256, FileName);//读取ini值，默认是COM1
            comboBox1.Text = temp.ToString();//初始化
        }

        TrackBar[] GetTrackBar()
        {
            return new TrackBar[] { trackBar1, trackBar2, trackBar3, trackBar4, trackBar5, 
                                       trackBar6, trackBar7, trackBar8, trackBar9, trackBar10,
            };//返回一个对象数组
        }

        private void PortDataReceivedEvent(object sender, SerialDataReceivedEventArgs args) //串口数据接收
        {
            TrackBar[] MytrackBar = GetTrackBar();
            byte[] Data = new byte[serialPort1.BytesToRead];
            serialPort1.Read(Data, 0, Data.Length);//读
            foreach (byte MyData in Data)
            {
                for (int i = 1; i < 10; i++)
                {
                    MytrackBar[10 - i].Value = MytrackBar[10 - i - 1].Value;
                }
                trackBar1.Value = (int)MyData;
                string str = MyData.ToString("x");
                //textBox1.Text  = "0x" + (str.Length == 1 ? "0" + str : str); 
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)//判断
            {
                groupBox2.Visible = false; //第二个框框不可见
                this.Size = new Size(620, 107); //缩小
                serialPort1.Close();//此处可不加try，catch
                TrackBar[] MyProgeressBar = GetTrackBar();
                foreach (TrackBar theBar in MyProgeressBar)//遍历，每个trackBar归零
                {
                    theBar.Value = 0;
                }
                button1.Text = "打开串口";
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;//串口号
                    serialPort1.Open(); //打开
                    CurrentPortName = comboBox1.Text;
                    groupBox2.Visible = true; //第二个框框可见
                    this.Size = new Size(620, 436); //放大
                    button1.Text = "关闭串口";//按键标题
                }
                catch
                {
                    MessageBox.Show("串口打开错误", "错误");
                }
            }

        }
    }
}
